package com.zcaxer.way.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.zcaxer.way.Listeners
import com.zcaxer.way.data.Building
import com.zcaxer.way.databinding.BuildingItemBinding
import com.zcaxer.way.viewmodels.BuildingViewModel


class BuildingAdapter(val viewModel: BuildingViewModel, val listeners: Listeners) :
        ListAdapter<Building, BuildingAdapter.BuildingViewHolder>(BuildingDiffCallback()) {


    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): BuildingViewHolder {
        val view = BuildingItemBinding.inflate(LayoutInflater.from(viewGroup.context), viewGroup, false)
        return BuildingViewHolder(view)
    }

    override fun onBindViewHolder(holder: BuildingViewHolder, position: Int) {
        val building = getItem(position)
        holder.bind(building, viewModel, listeners)
        holder.numberPicker.value = building.workers
        holder.numberPicker.setOnValueChangedListener(listeners.numberPickerListener(viewModel, building))
    }
    /*
    initData(holder, building)
    holder.buttonUpgrade.setOnClickListener {
        building.upgrade()
        initData(holder, building)
        setTVneed(holder, building)
    }

    holder.editTextTarget.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, c: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence, c: Int, before: Int, count: Int) {

        }

        override fun afterTextChanged(s: Editable) {
            try {
                target = Integer.parseInt(holder.editTextTarget.text.toString())
            } catch (e: NumberFormatException) {
                target = 1
            }

            setTVneed(holder, building)
        }
    })

    holder.numberPicker.setOnValueChangedListener { picker, oldVal, newVal ->
        building.setWorkers(newVal)
        setWorkersData(holder, building)
        setTVneed(holder, building)
    }
*/


    /*private fun setWorkersData(buildingViewHolder: BuildingViewHolder, building: Building) {
        buildingViewHolder.textViewWorks.text = "仆役：" + building.workers + "/" + building.maxWorkers
        buildingViewHolder.numberPicker.maxValue = building.maxWorkers
        buildingViewHolder.numberPicker.value = building.workers
    }

    private fun setReserveData(buildingViewHolder: BuildingViewHolder, building: Building) {
        buildingViewHolder.textViewReserveAndCapacity.text = "储量：" + building.reserve + "/" + building.capacity
    }

    private fun initData(buildingViewHolder: BuildingViewHolder, building: Building) {
        buildingViewHolder.textViewBuildingName.text = building.type.str
        buildingViewHolder.textViewBuildingLevel.text = building.level.toString() + "级"
        setWorkersData(buildingViewHolder, building)
        setReserveData(buildingViewHolder, building)
    }
*/
    /*
    private fun setTVneed(buildingViewHolder: BuildingViewHolder, building: Building) {
        var s = "未计算"
        val time: Int
        if (building.workers == 0)
            s = "仆役为0"
        else {
            if (target > building.capacity)
                s = "超出上限"
            else if (building.name == "foodProducer")
                s = "食物无需计算"
            else if (target < building.reserve)
                s = "需要" + (target - building.reserve).toDouble() / 360.0 / building.workers.toDouble() + "h"
            else if (target > building.reserve) s = "已满足"
        }
        buildingViewHolder.textViewTargetNeed.text = s
    }
    */


    class BuildingViewHolder(private val binding: BuildingItemBinding)
        : RecyclerView.ViewHolder(binding.root) {
        val numberPicker = binding.numberPicker
        fun bind(building: Building, viewModel: BuildingViewModel, listeners: Listeners) {
            binding.apply {
                this.viewModel = viewModel
                this.building = building
                this.listener = listeners
                executePendingBindings()
            }
        }

    }

    class BuildingDiffCallback : DiffUtil.ItemCallback<Building>() {
        /**
         * Called to check whether two items have the same data.
         *
         *
         * This information is used to detect if the contents of an item have changed.
         *
         *
         * This method to check equality instead of [Object.equals] so that you can
         * change its behavior depending on your UI.
         *
         *
         * For example, if you are using DiffUtil with c
         * [RecyclerView.Adapter], you should
         * return whether the items' visual representations are the same.
         *
         *
         * This method is called only if [.areItemsTheSame] returns `true` for
         * these items.
         *
         *
         * Note: Two `null` items are assumed to represent the same contents. This callback
         * will not be invoked for this case.
         *
         * @param oldItem The item in the old list.
         * @param newItem The item in the new list.
         * @return True if the contents of the items are the same or false if they are different.
         *
         * @see Callback.areContentsTheSame
         */
        override fun areContentsTheSame(oldItem: Building, newItem: Building): Boolean {
            return oldItem == newItem
        }

        /**
         * Called to check whether two objects represent the same item.
         *
         *
         * For example, if your items have unique ids, this method should check their id equality.
         *
         *
         * Note: `null` items in the list are assumed to be the same as another `null`
         * item and are assumed to not be the same as c non-`null` item. This callback will
         * not be invoked for either of those cases.
         *
         * @param oldItem The item in the old list.
         * @param newItem The item in the new list.
         * @return True if the two items represent the same object or false if they are different.
         *
         * @see Callback.areItemsTheSame
         */
        override fun areItemsTheSame(oldItem: Building, newItem: Building): Boolean {
            return oldItem.ID == newItem.ID
        }
    }


}
